<?php

namespace app\api\model;

use app\common\model\BaseModel;
use think\facade\Db;


class Team extends BaseModel
{


    // 表名
    protected $name = 'match';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;


    public function getList($user)
    {
        $userList = User::where(['pid' => $user['id']])->order('id desc')
            ->field('id,username,mobile,avatar,createtime')
            ->select()->toArray();


        $teamIncome = Db::name('user_team')->where(['pid' => $user['id']])
            ->field('SUM(income) as total_income,user_id')->select()->toArray();
        $teamIncome = array_column($teamIncome,'total_income','user_id');
        foreach ($userList as &$item) {
            //查询该用户的收入
            $item['total_income'] = $teamIncome[$item['id']] ?? 0.00;
            $item['create_time'] = date('Y-m-d H:i:s',$item['createtime']);
        }


        return ['status' => 1, 'message' => '操作成功', 'result' => ['list' => $userList,'count' =>count($userList)]];
    }



    public function getIncome($user){
        $userList =Db::name('user_team')->alias('t')
            ->leftJoin('user u','u.id = t.user_id')
            ->where(['t.pid' => $user['id']])
            ->field('t.*,u.username,u.mobile,u.avatar')->select()->toArray();

        foreach ($userList as &$value){
            $value['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
        }

        return ['status' => 1, 'message' => '操作成功', 'result' => ['list' => $userList,'user' =>$user]];

    }

}
